﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using SharpDom;
using System.Web.Mvc.Html;
using Bzure.Apps.Security.ViewModels;

namespace Bzure.Apps.Security.Views
{
    public class NewRole : PageView<NewRoleViewModel>
    {
        public virtual ContainerTag TitleContent(ContainerTag container)
        {
            return container[Model.Title];
        }
        string Script1()
        {
            StringBuilder sb = new StringBuilder();
            sb.Append(" $().ready(function () {");
            sb.Append(" $(\"#NewRoleForm\").validate({");
            sb.Append("rules: {RoleName: {required: true, minlength: 2},");
            sb.Append("RoleDescription: {required: true, minlength: 5}},");
            sb.Append("messages: {RoleName: {required: \"请输入角色名\", minlength: \"角色名至少需要两个字符\"},");
            sb.Append("RoleDescription: {required: \"请输入角色描述\", minlength: \"角色描述至少需要5个字符\"}}");
            sb.Append("  });});");
            return sb.ToString();
        }
        string Script2(string url)
        {
            StringBuilder sb = new StringBuilder();
            sb.AppendFormat("var url =\"{0}\";", url);
            sb.Append("  var CreateRole = function () {");
            sb.Append(" var form = $(\"#NewRoleForm\");");
            sb.Append("if (form.valid()) {");
            sb.Append("if (confirm(\"你确定要创建该角色吗？\")) {");
            sb.Append("var serializedForm = form.serialize();");
            sb.Append(" $.post(url, serializedForm, function (result) {");
            sb.Append("  $(\"#content_box_messages\").html(result);");
            sb.Append("$(\"#RoleName\").val(\"\");");
            sb.Append(" $(\"#RoleDescription\").val(\"\");");
            sb.Append(" $(\".close\").click(");
            sb.Append("function () {");
            sb.Append(" $(this).fadeTo(400, 0, function () {");
            sb.Append("$(this).slideUp(400);");
            sb.Append("  });");
            sb.Append(" return false;");
            sb.Append("});");
            sb.Append(" });");
            sb.Append("} }};");
            return sb.ToString();
        }
        public virtual ContainerTag MainContent(ContainerTag container)
        {
            return container
            [
                 script.attr(type: MimeType.text.javascript, src: this.Url.RouteUrl("ResourceAccess_Script", new { location = "SecurityApp", id = "jquery-1.7.2.min.js" })),
                script.attr(type: MimeType.text.javascript, src: this.Url.RouteUrl("ResourceAccess_Script", new { location = "SecurityApp", id = "jquery.validate.min.js" })),
                script.attr(type: MimeType.text.javascript)[Script2(Model.Url)],
                script.attr(type: MimeType.text.javascript)[Script1()],
                h2[Model.Title],
                p.attr(id: "page-intro")["创建一个新的安全角色，请确保所有角色的名称是唯一的。"],
                div.attr(@class: "clear"),
                div.attr(@class: "content-box")
                [
                     div.attr(@class: "content-box-header")
                      [
                            h3["新角色"],
                            ul.attr(@class: "content-box-tabs")
                            [
                               li[a.attr(href: "#tab1", @class: "default-tab")["创建"]]
                            ],
                            div.attr(@class: "clear")
                      ],
                     div.attr(@class: "content-box-content")
                      [
                          div.attr(@class: "tab-content default-tab", id: "tab1")
                          [
                                div.attr(id:"content_box_messages"),
                                form.attr(id: "NewRoleForm")
                                [
                                       p.attr(id: "NewRole-RoleName")
                                        [
                                             label.attr(@for: "RoleName")["角&nbsp;&nbsp;色&nbsp;名："],
                                             input.attr(type: InputType.text, id: "RoleName", name: "RoleName", maxlength: 20)
                                        ],
                                        p.attr(id: "NewRole-RoleDescription")
                                        [
                                             label.attr(@for: "RoleDescription")["角色描述："],
                                             input.attr(type: InputType.text, id: "RoleDescription", name: "RoleDescription", maxlength: 50)
                                        ],
                                         p.attr(id: "NewRole-submit")
                                        [
                                             input.attr(type: InputType.button, @class: "button", value: "创建", onclick: "CreateRole();")
                                        ]
                                ]
                          ]
                      ]
                ]
            ];
        }

        public override ContainerTag CreateTagTree(ContainerTag container)
        {
            return ReusableCreateTagTree(TitleContent, MainContent, Model.MasterViewModel);
        }
    }
}
